package mof.gov.dmdc.view.backing.lov;

import java.util.ArrayList;
import java.util.List;

import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

import mof.gov.dmdc.model.applicationmodule.lovModuleImpl;
import mof.gov.dmdc.view.common.Common;

import oracle.adf.view.rich.component.rich.input.RichSelectOneChoice;

import oracle.jbo.Row;
import oracle.jbo.ViewCriteria;
import oracle.jbo.ViewCriteriaRow;
import oracle.jbo.ViewObject;

public class SearchLov {
    private List tinhList;
    private List huyenList;
    private String maSearch;
    private String tenSearch;
    private boolean renderedHuyen;
    private boolean renderedTinh;
    private RichSelectOneChoice tinhSoc;
    private String tinhValue;
    private String huyenValue;
    private static String LOV_ITERATOR = "lovVOIterator";

    private void initListTinh() {
        lovModuleImpl am = (lovModuleImpl)Common.getAppModuleForDataControl("lovModuleDataControl");
        ViewObject vo = am.getTinhVO();
        tinhList = new ArrayList();
        do {
            Row row = vo.hasNext() ? vo.next() : vo.first();
            if (row != null) {
                String ma = row.getAttribute("MaT").toString();
                String ten = row.getAttribute("Ten").toString();
                SelectItem item = new SelectItem(ma, ten);
                tinhList.add(item);
            }
        } while (vo.hasNext());
        SelectItem firstItem = (SelectItem)tinhList.get(0);
        tinhValue = (String)firstItem.getValue();
        System.out.println(tinhValue);
    }

    public void createHuyenList(String maTinh) {
        if (maTinh.length() == 1) {
            maTinh = "0" + maTinh;
        }
        lovModuleImpl am = (lovModuleImpl)Common.getAppModuleForDataControl("lovModuleDataControl");
        ViewObject vo = am.getHuyenVO();
        vo.setNamedWhereClauseParam("maTinh", maTinh);
        if (huyenList != null && (!huyenList.isEmpty())) {
            huyenList.clear();
        } else {
            huyenList = new ArrayList();
        }
        vo.executeQuery();
        while (vo.hasNext()) {
            Row row = vo.next();
            if (row != null) {
                String ma = row.getAttribute("MaH").toString();
                String ten = row.getAttribute("Ten").toString();
                SelectItem item = new SelectItem(ma, ten);
                huyenList.add(item);
            }
        }
        SelectItem firstItem = (SelectItem)huyenList.get(0);
        huyenValue = (String)firstItem.getValue();
    }

    public void initSearchForLOV() {
        String lovName = Common.getFromPageFlowScope("lovName").toString();
        if (lovName.equalsIgnoreCase("lovmaxa")) {
            renderedHuyen = true;
            renderedTinh = true;
            initListTinh();
            createHuyenList(tinhValue);
        } else {
            if (lovName.equalsIgnoreCase("lovmahuyen")) {
                renderedHuyen = false;
                renderedTinh = true;
                initListTinh();
            } else {
                renderedHuyen = false;
                renderedTinh = false;
            }
        }
    }

    public void setTinhList(List tinhList) {
        this.tinhList = tinhList;
    }

    public List getTinhList() {
        return tinhList;
    }

    public void setHuyenList(List huyenList) {
        this.huyenList = huyenList;
    }

    public List getHuyenList() {
        return huyenList;
    }

    public void setRenderedHuyen(boolean renderedHuyen) {
        this.renderedHuyen = renderedHuyen;
    }

    public boolean isRenderedHuyen() {
        return renderedHuyen;
    }

    public void setRenderedTinh(boolean renderedTinh) {
        this.renderedTinh = renderedTinh;
    }

    public boolean isRenderedTinh() {
        return renderedTinh;
    }

    public void setTinhSoc(RichSelectOneChoice tinhSoc) {
        this.tinhSoc = tinhSoc;
    }

    public RichSelectOneChoice getTinhSoc() {
        return tinhSoc;
    }

    public void setTinhValue(String tinhValue) {
        this.tinhValue = tinhValue;
    }

    public String getTinhValue() {
        return tinhValue;
    }

    public void setMaSearch(String maSearch) {
        this.maSearch = maSearch;
    }

    public String getMaSearch() {
        return maSearch;
    }

    public void setTenSearch(String tenSearch) {
        this.tenSearch = tenSearch;
    }

    public String getTenSearch() {
        return tenSearch;
    }

    public void resetActionListener(ActionEvent actionEvent) {
        actionEvent.getComponent();
        maSearch = "";
        tenSearch = "";
        Common.refreshComponent(Common.getComponentFromRoot("it1"));
        Common.refreshComponent(Common.getComponentFromRoot("it2"));
    }

    public void selectTinhChangeListener(ValueChangeEvent valueChangeEvent) {
        String newValue;
        try {
            newValue = valueChangeEvent.getNewValue().toString();
        } catch (Exception e) {
            newValue = null;
        }

        if (newValue != null) {
            if (renderedHuyen) {
                createHuyenList(newValue);
                Common.refreshComponent(Common.getComponentFromRoot("soc2"));
            } else {
                ViewObject vo = Common.getViewObjectForIterator(LOV_ITERATOR);
                ViewCriteria criteria = vo.createViewCriteria();
                ViewCriteriaRow criteriaRow = criteria.createViewCriteriaRow();
                criteriaRow.setAttribute(2, newValue);
                criteriaRow.setOperator(2, "like");
                criteria.addRow(criteriaRow);
                vo.applyViewCriteria(criteria);
                vo.executeQuery();
                Common.refreshComponent(Common.getComponentFromRoot("t1"));
            }
        }
    }

    public void selectHuyenChangeListener(ValueChangeEvent valueChangeEvent) {
        String newValue;
        try {
            newValue = valueChangeEvent.getNewValue().toString();
        } catch (Exception e) {
            newValue = "";
        }
        ViewObject lovVO = Common.getViewObjectForIterator(LOV_ITERATOR);
        ViewCriteria criteria = lovVO.createViewCriteria();
        try {
            lovVO.getViewCriteria().clear();
        } catch (Exception e) {
            System.out.println("Cha co thang criteria nao uh!");
        }
        if (!newValue.isEmpty()) {
            ViewCriteriaRow criteriaRow = criteria.createViewCriteriaRow();
            criteriaRow.setAttribute(2, newValue);
            criteriaRow.setOperator(2, "like");
            criteria.addRow(criteriaRow);
            lovVO.applyViewCriteria(criteria);
            lovVO.executeQuery();
        } else {
            lovVO.executeQuery();
        }
        Common.refreshComponent(Common.getComponentFromRoot("t1"));
    }

    public void searchActionListener(ActionEvent actionEvent) {
        actionEvent.getComponent();
        ViewObject vo = Common.getViewObjectForIterator(LOV_ITERATOR);
        ViewCriteria criteria = vo.createViewCriteria();
        ViewCriteriaRow criteriaRow = criteria.createViewCriteriaRow();
        if (maSearch != null) {
            criteriaRow.setAttribute(0, "%" + maSearch + "%");
            criteriaRow.setOperator(0, "like");
        }
        if (tenSearch != null) {
            criteriaRow.setAttribute(1, "%" + tenSearch.toUpperCase() + "%");
            criteriaRow.setOperator(1, "like");
            criteriaRow.setUpperColumns(true);
        }
        criteria.addRow(criteriaRow);
        vo.applyViewCriteria(criteria);
        vo.executeQuery();
        Common.refreshComponent(Common.getComponentFromRoot("t1"));
    }

    public void setHuyenValue(String huyenValue) {
        this.huyenValue = huyenValue;
    }

    public String getHuyenValue() {
        return huyenValue;
    }
}
